package logo.sys;

import java.awt.Color;

import logo.lang.*;

public class Turtle 
{
    boolean penIsDown;		// false = up, true = down
    boolean isVisible;		// true if turtle is visible on display
    double xcord;		// x coordinate of position
    double ycord;		// y coordinate of position
    double heading;		// direction turtle faces in degrees
    
    public static final int CANVAS_SIZE = 400;

    final double cfactor = (Math.PI / 180); // degrees to radians factor

    //  MyCanvas canvas = new MyCanvas(600, 600);
    TurtleDisplay canvas;
  
    /** Create a new Turtle object in the given state */
    public Turtle(TurtleDisplay td, boolean s, double x, double y, double h) {

	canvas = td;
	penIsDown = s;
	xcord = x;
	ycord = y;
	heading = h;	
	isVisible = true;
	try {
	    Thread.currentThread();
		Thread.sleep(1000);
	    td.showTurtle(h * cfactor, x, y);
	} catch (InterruptedException ie) {
	}
    }

    /** Create a new Turtle object that is at the origin, with a heading of
	zero (towards the top of the screen).
    */
    public Turtle() {
	this(new TurtleDisplay(), true, 0, 0, 0);
    }

    /** Return the X coordinate of the turtle. */
    public double getX() {
	return xcord;
    }

    /** Return the Y coordinate of the turtle. */
    public double getY() {
	return ycord;
    }

    /** Return the current heading of the turtle as an angle in degrees. */
    public double getHeading() {
	return heading;
    }

    /** Return the display upon which the turtle and its steps are drawn */
    public TurtleDisplay getDisplay() {
	return canvas;
    }

    /** Change the size of the display */
    public void setDisplaySize(int width, int height) {
	canvas.setSize(width, height);
    }

    protected void drawTurtle() {
	canvas.showTurtle(heading * cfactor, xcord, ycord);
    }
    
    /** Set the turtle to be visible on the display */
    public void show() {
	isVisible = true;
	drawTurtle();
    }

    /** Set the turtle to be invisible on the display */
    public void hide() {
	isVisible = false;
	canvas.hideTurtle();
    }

    /** Set the heading of this turtle (value given in degrees). */
    public void setHeading(double h) {
	heading = h % 360;
	if (isVisible) drawTurtle();
    }

    /** Set the coordinates of this turtle. */
    public void setPosition(double x, double y) {
	xcord = x;
	ycord = y;
	if (isVisible) drawTurtle();
    }
    
    /** Rotate the turtle right by the specified angle (in degrees). */
    public void right(double angle) throws LogoException {
	heading = (heading + angle) % 360;
	if (isVisible) drawTurtle();
    }

    /** Rotate the turtle left by the specified angle (in degrees). */
    public void left(double angle) throws LogoException {
	heading = (heading - angle) % 360;
	if (isVisible) drawTurtle();
    }

    /** Move the turtle by the given distance along its current heading. */
    public void forward(double len) throws LogoException {
	double angle = heading * cfactor;
	double xcord1, ycord1;
	xcord1 = xcord + len * Math.sin(angle);
	ycord1 = ycord + len * Math.cos(angle);

	if (penIsDown)
	    canvas.drawSegment(xcord, ycord, xcord1, ycord1);

	xcord = xcord1;
	ycord = ycord1;

	if (isVisible) drawTurtle();
    }

    /** Move the turtle back by the given distance along its current heading.
     */
    public void back(double len) throws LogoException {
	double angle = heading * cfactor;
	double xcord1, ycord1;
	xcord1 = xcord - len * Math.sin(angle);
	ycord1 = xcord - len * Math.cos(angle);

	if (penIsDown)
	    canvas.drawSegment(xcord, ycord, xcord1, ycord1);

	xcord = xcord1;
	ycord = ycord1;

	if (isVisible) drawTurtle();
    }

    /** Restore the turtle to its home configuration
	(at the origin pointing up).
    */
    public void home() {
	penIsDown = true;
	heading = 0;
	xcord = 0;
	ycord = 0;
	if (isVisible) drawTurtle();
    }

    /** Raise the pen, so that no lines are drawn as the turtle moves */
    public void penUp() {
	penIsDown = false;
    }

    /** Lower the pen, so that a line is drawn as the turtle moves. */
    public void penDown() {
	penIsDown = true;
    }

    /** Clear the display */
    public void clear() {
	canvas.clear();
	if (isVisible) drawTurtle();
    }

    /** Set the colour used by the pen. */
    public void setPenColour(LogoVal red, LogoVal green, LogoVal blue)
	throws LogoException {
	canvas.setForeground(new Color(red.intValue(), green.intValue(),blue.intValue()));
	canvas.repaint();
    }

    /** Set the background colour of the display. */
    public void setBackColour(LogoVal red, LogoVal green, LogoVal blue)
	throws LogoException {
	canvas.setBackground(new Color(red.intValue(), green.intValue(), blue.intValue()));
    canvas.repaint();
    }
  /*  
    public void stop () {
    	
    }
  */
}
